﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Xml.Linq;
using TongYI_Lingma_UWP_.Control;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace TongYI_Lingma_UWP_.Pages
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class CommentPage : Page
    {
        public CommentPage()
        {
            this.InitializeComponent();
            AddChatHistory(); //添加聊天记录
        }

        /// <summary>
        /// 添加聊天记录
        /// </summary>
        private void AddChatHistory()
        {
            // 假设 ChatHistoryList 是一个 ListView 或 ListBox 控件
            foreach (var item in Directory.GetFiles("ChatHistory"))
            {
                if (Path.GetExtension(item).ToLower() == ".xml")
                {
                    XDocument xmlDoc = XDocument.Load(item);
                    XElement titleElement = xmlDoc.Descendants("Title").FirstOrDefault();
                    XElement messageElement = xmlDoc.Descendants("Messages").FirstOrDefault();

                    if (titleElement != null && messageElement != null)
                    {
                        string fileName = Path.GetFileNameWithoutExtension(item); // 文件名
                        string title = titleElement.Value; // 获取title
                        string message = messageElement.Value; // 获取message

                        ChatList chatList = new ChatList();
                        chatList.Title = fileName;
                        chatList.Massage = title; // 注意这里应该是 title 而不是 message
                        ChatHistoryList.Items.Add(chatList);
                    }
                }
            }
        }

        /// <summary>
        /// 点击添加聊天记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private  void AddChatButton_Click(object sender, RoutedEventArgs e)
        {
          //新建TXT文件
          //保存到ChatHistory文件夹里面
          //保存到ChatHistory文件夹下
          string fileName = "ChatHistory" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";// 文件名
            string FileContent = "hello";// 文件内容
            try
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(FileContent);
                sw.Close();
                fs.Close();
            }
            catch (Exception )
            {
                
            }
                                  
               
        }
    }
}
